<?php
require_once 'global.inc.php';
require_once 'classes/class.upload.php';
require_once 'classes/resize-class.php';

//check to see if they're logged in
if(!isset($_SESSION['admin_logged_in'])) {
	header("Location: index.php");
}

//get the user object from the session
$user = unserialize($_SESSION['admin_user']);
if (!empty($_POST['submit'])) {
	$productName = $_POST['name'];
	$productPrice = $_POST['price'];
	//$productPicture = $_POST['picture'];
	$productDesc = $_POST['description'];
	$productType = $_POST['type'];
	$productUnitInStock = $_POST['unitInStock'];
	$productUnitSold = $_POST['unitSold'];

	// upload image
	$uploader = new Uploader();
	$uploader->setDir(BASE_PATH.'upload/');
	$uploader->setExtensions(array('jpg', 'jpeg', 'png', 'gif'));  //allowed extensions list//
	$uploader->setMaxSize(5);  //set max file size to be allowed in MB//
	$uploader->setSequence('Product');
	$uploader->setSameFileName(FALSE);
	if ($uploader->uploadFile('picture')) {   //txtFile is the filebrowse element name //
		// *** 1) Initialise / load image
		$resizeObj = new resize(BASE_PATH.'upload/'.$uploader->getUploadName());
		// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
		$resizeObj -> resizeImage(200, 200, 'crop');
		// *** 3) Save image
		$resizeObj -> saveImage(BASE_PATH.'upload/'.$uploader->getUploadName(), 100);
		$productPicture = $uploader->getUploadName();
	} else {//upload failed
		echo $uploader->getMessage();  //get upload error message
		$productPicture = '';
	}

	$table = 'items';
	$newProduct = array(
		'item_name' => $productName,
		'price' => $productPrice,
		'picture' => $productPicture,
		'description' => $productDesc,
		'type' => $productType,
		'unit_instock' => $productUnitInStock,
		'unit_sold' => $productUnitSold
	);
	$insertId = $GLOBALS['DB']->insert($newProduct, $table);
	if ($insertId) {
		header("Location: index.php");
	}
}

?>
<!DOCTYPE html>
<html>
	<?php include 'header.php';?>
	<body>
		<!-- Navigation start -->
		<div class="navbar navbar-inverse navbar-fixed-top">
			<div class="navbar-inner">
				<div class="container">
					<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
				</div>
			</div>
		</div>
		<!-- Navigation end -->

		<!-- Content start -->
		<div class="container">
			<div class="row">
				<div class="span12">
					
				</div>
			</div>
		</div>


		<!-- Content end -->
		<?php include 'footer.php';?>
		<!-- Javascript -->
		<script src="js/jquery.js"></script>
		<script src="js/bootstrap.min.js"></script>
	</body>
</html>